#define _CRT_SECURE_NO_WARNINGS
#include"sq.h"


void HpInit(Hp* ph)
{
	assert(ph);
	ph->pa = NULL;
	ph->size = 0;
	ph->cpycity = 0;
}
void HpDestroyed(Hp* ph)
{
	assert(ph);
	assert(ph->pa);
	free(ph->pa);
	ph->pa = NULL;
}

void Swap(Hptyp* child, Hptyp* parent)
{
	Hptyp tmp = *child;
	*child = *parent;
	*parent = tmp;

}

void AdjustUp(Hptyp* pa, int child) {

	int parent = (child - 1) / 2;
	while (child > 0)
	{
		if (pa[child] < pa[parent])
		{
			Swap(&pa[child], &pa[parent]);
			child = parent;
			parent = (child - 1) / 2;

		}

		else
			break;
	}

}

void HpPush(Hp* ph, int x)
{

	if (ph->size == ph->cpycity)
	{
		int newcpycity = ph->cpycity == 0 ? 4 : 2 * ph->cpycity;
		Hptyp* tmp = (Hptyp*)realloc(ph->pa, newcpycity * sizeof(Hptyp));

		if (tmp == NULL)
		{
			perror("realloc fail");
			return;
		}

		ph->pa = tmp;
		ph->cpycity = newcpycity;

	}

	ph->pa[ph->size] = x;

	ph->size++;

	AdjustUp(ph->pa, ph->size - 1);
}
void HpPop(Hp * ph );